<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="hAdd">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" width="200" label="启用">
          <template slot-scope="{row}">

            <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
            <span v-else>
              {{ row.state === 1 ? '已启用' : '未启用' }}
            </span>

          </template>
        </el-table-column>

        <el-table-column prop="description" align="center" label="描述">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button type="primary" size="mini" @click="hOk(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit = false">取消</el-button>
            </template>
            <template v-else>
              <!-- 非编辑状态 -->
              <el-button size="mini" type="text" @click="hAssignPerm(row.id)">分配权限</el-button>
              <el-button size="mini" type="text" @click="hEidt(row)">编辑</el-button>
              <!-- <el-button size="mini" type="text" @click="hDel(row.id)">删除</el-button> -->
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容确定删除吗？"
                @onConfirm="hDel(row.id)"
              >
                <el-button slot="reference" type="text" size="small">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          background
          layout="sizes, prev, pager, next"
          :total="total"
          :current-page.sync="page"
          :page-size="pagesize"
          :page-sizes="[5, 10, 15, 20]"
          @current-change="hCurrentChage"
          @size-change="hSizeChange"
        />

      </el-row>
    </div>
    <el-dialog
      title="新增角色"
      :visible.sync=" showDialog"
      width="50%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="form.state" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="form.description" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="hSave">确定</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <AssignPermission ref="refPerm" :show-dialog-perm.sync="showDialogPerm" :cur-id="curId" />
  </div>
</template>
<script>
import { addRoles, getRoles, editRoles, delRoles } from '@/api/role'
import AssignPermission from './components/assignPermission.vue'
export default {
  name: 'Role',
  components: {
    AssignPermission
  },
  data() {
    return {
      curId: -1,
      showDialogPerm: false,
      list: [],
      total: 0,
      page: 1,
      pagesize: 5,
      showDialog: false,
      isEdit: true,
      value: '',
      form: {
        name: '',
        description: '',
        state: 0 // 默认未1启用 关闭 0 打开1
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    hAssignPerm(id) {
      this.curId = id
      this.showDialogPerm = true
      this.$nextTick(() => {
        this.$refs.refPerm.getPermiss()
      })
    },
    async hDel(id) {
      await delRoles(id)
      this.$message.success('删除成功')
      if (this.list.length === 1) {
        if (this.page > 1) {
          this.page--
        }
      }
      this.loadRoles()
    },
    async hOk(row) {
      if (!row.editRow.name || !row.editRow.description) {
        this.$message.error('角色和描述不能为空')
        return
      }
      console.log(row.editRow)
      await editRoles(row.editRow)
      this.$message.success('更新角色成功')
      // eslint-disable-next-line require-atomic-updates
      row.isEdit = false
      this.loadRoles()
    },
    hEidt(row) {
      console.log(row)
      row.isEdit = true
    },
    async doAdd() {
    //  调用接口函数
      await addRoles(this.form)
      // 提示成功
      this.$message.success('添加成功')
      // 关闭弹框
      this.showDialog = false
      // 清空表单
      this.$refs.form.resetFields()

      // 自动跳到最后一页
      // this.total = this.total + 1
      // this.page = Math.ceil(this.total / this.pagesize)
      this.page = this.total / this.pagesize + 1
      console.log(this.page, this.total, this.pagesize)
      // 更新表单数据
      this.loadRoles()
    },
    hSave() {
      this.doAdd()
    },
    hAdd() {
      this.showDialog = true
    },
    // 用户更改每页显示几条数据
    hSizeChange(pages) {
    //  更新每页的条数
      this.pagesize = pages
      // 更新页码为1
      this.page = 1
      // 重新发请求
      this.loadRoles()
    },
    hCurrentChage(page) {
    // 更新页面
      this.page = page
      // 重发请求
      this.loadRoles()
    },
    async loadRoles() {
    // page : 当前页码数
    // pagesize : 当前页面需要的数据条数
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })
      console.log(res.data.rows)
      this.list = res.data.rows
      this.list.forEach(it => {
        this.$set(it, 'isEdit', false)
        this.$set(it, 'editRow', {
          id: it.id,
          name: it.name,
          state: it.state,
          description: it.description
        })
      })
      this.total = res.data.total
    }

  }

}

</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>
